package com.xintoucloud.pbs.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.xintoucloud.pbs.dao.OrganizationDao_1;
import com.xintoucloud.pbs.entity.Organization;
@Repository@Transactional
public class OrganizationDaoImpl implements OrganizationDao_1{
	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Organization> listOrganizationsIsZeroParentId() {
		return sessionFactory.getCurrentSession().createQuery("from Organization where parentId=?0").setParameter(0, 0).getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Organization> listOrganizationsByParentId(Integer parentId) {
		return sessionFactory.getCurrentSession().createQuery("from Organization where parentId=?0").setParameter(0, parentId).getResultList();
	}

	@Override
	public void deleteOrganizationById(int oid) {
		Organization organization=(Organization) sessionFactory.getCurrentSession().createQuery("from Organization where oid=?0").setParameter(0, oid).getSingleResult();
		sessionFactory.getCurrentSession().delete(organization);
		return;
	}

	@Override
	public boolean saveOrganization(Organization organization) {
		return sessionFactory.getCurrentSession().save(organization) != null;
	}

	@Override
	public void updateOrganization(Organization organization) {
		sessionFactory.getCurrentSession().createQuery("update Organization set oname='"+organization.getOname()+"',bname='"+organization.getBname()+"',linkman='"+organization.getLinkman()+"',ophone='"+organization.getOphone()+"',address='"+organization.getAddress()+"',membersList='"+organization.getMembersList()+"',coordinate='"+organization.getCoordinate()+"' where oid='"+organization.getOid()+"'").executeUpdate();
	}

	@Override
	@Transactional(readOnly=true)
	public Organization getOne(Integer oid) {
		return sessionFactory.getCurrentSession().get(Organization.class, oid);
	}

	@Override
	public void updateOrganization(int oid,String oName) {
		sessionFactory.getCurrentSession().createQuery("update Organization o set o.oname='"+oName+"' where o.oid='"+oid+"'").executeUpdate();
	}

	@Override
	public List<String> listOrganizationMap() {
		@SuppressWarnings("unchecked")
		List<String> list=sessionFactory.getCurrentSession().createQuery("select o.coordinate from Organization o").list();
		return list;
	}

}
